/**
 * 
 */
package sudoku.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import sudoku.controller.Controller;
import sudoku.model.ChoiceModel;
import sudoku.model.SudokuModel;
import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author Romet
 *
 */
/**
 * @author Raili
 * 
 */
public class ChoiceUI implements ActionListener, WindowListener, KeyListener, MouseListener {

  private volatile Config config;
  private volatile SudokuUI m2ng;
  private volatile ChoiceModel choiceModel;
  private volatile Controller controller;
  private volatile SudokuModel model;
  private volatile Language language;

  private JDialog dialog;

  private JButton[] valik;

  /**
   * Seadistab valiku kasutajaliidese.
   * 
   * @param config
   *          konfiguratsiooni klassi isend
   * @param m2ng
   *          põhiakna graafika klassi isend
   */
  public ChoiceUI(Language language, Config config, SudokuUI m2ng) {
    this.language = language;
    this.config = config;
    this.m2ng = m2ng;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < valik.length; i++) {
      if (e.getSource().equals(valik[i])) {
        if (valik[i].getForeground().equals(config.getEsialgne())) {
          return;
        }
        choiceModel.setValik_i(i);
        controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
      }
    }
    dialog.dispose();
  }

  public void deleteKey() {
    choiceModel.setValik_i(0);
    controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
    dialog.dispose();
  }

  /**
   * Tagastab valikuakna.
   * 
   * @return valikuaken
   */
  public JDialog getDialog() {
    return dialog;
  }

  @Override
  public void keyPressed(KeyEvent e) {

  }

  @Override
  public void keyReleased(KeyEvent e) {
  }

  @Override
  public void keyTyped(KeyEvent e) {
    if (e.getKeyChar() == KeyEvent.VK_DELETE) {
      deleteKey();
    }
    if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
      dialog.dispose();
      m2ng.uuendaUI();
      m2ng.uuendaBackground(model.getValik_i(), model.getValik_j());
    }
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == 3) {
      dialog.dispose();
      m2ng.uuendaUI();
      m2ng.uuendaBackground(model.getValik_i(), model.getValik_j());
    }
  }

  @Override
  public void mouseEntered(MouseEvent arg0) {
  }

  @Override
  public void mouseExited(MouseEvent arg0) {
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
  }

  @Override
  public void mouseReleased(MouseEvent arg0) {
  }

  public void setChoiceModel(ChoiceModel choiceModel) {
    this.choiceModel = choiceModel;
  }

  public void setController(Controller controller) {
    this.controller = controller;
  }

  public void setModel(SudokuModel model) {
    this.model = model;
  }

  /**
   * Teeb valikuakna.
   */
  public void valik() {
    valik = new JButton[config.getRuudustik() + 1];
    if (model.isAbi()) {
      dialog = new JDialog(m2ng, language.getChoice_titleC(), false);
    }
    else {
      dialog = new JDialog(m2ng, language.getChoice_titleH(), false);
    }
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(config.getKast(), config.getKast()));
    List<Integer> options = new ArrayList<Integer>();
    for (int i = 1; i < valik.length; i++) {
      valik[i] = new JButton(choiceModel.getChoiceButton()[i].getNumber());
      valik[i].setPreferredSize(new Dimension(50, 50));
      valik[i].setFocusPainted(false);
      valik[i].setForeground(choiceModel.getChoiceButton()[i].getV2rv());
      if (choiceModel.getChoiceButton()[i].getV2rv().equals(config.getLahendus())) {
        options.add(i);
      }
      valik[i].setBackground(config.getChoiceButton());
      valik[i].addActionListener(this);
      valik[i].addKeyListener(this);
      valik[i].addMouseListener(this);
      panel.add(valik[i]);
    }
    dialog.add(panel);
    dialog.pack();
    Point locCurs = MouseInfo.getPointerInfo().getLocation();
    dialog.setLocation(locCurs.x - dialog.getWidth() / 2, locCurs.y - dialog.getHeight() / 2);
    if (options.size() == 1) {
      Point locValik = valik[options.get(0)].getLocation();
      locCurs = MouseInfo.getPointerInfo().getLocation();
      Point locDial = dialog.getLocation();
      locDial.x += locCurs.x - (locDial.x + locValik.x + valik[options.get(0)].getWidth() / 2);
      locDial.y += locCurs.y - (locDial.y + locValik.y + valik[options.get(0)].getHeight());
      dialog.setLocation(locDial);
    }
    dialog.addWindowListener(this);
    dialog.setVisible(true);
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
   */
  @Override
  public void windowActivated(WindowEvent arg0) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
   */
  @Override
  public void windowClosed(WindowEvent arg0) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
   */
  @Override
  public void windowClosing(WindowEvent arg0) {
    m2ng.uuendaUI();
    m2ng.uuendaBackground(model.getValik_i(), model.getValik_j());
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
   */
  @Override
  public void windowDeactivated(WindowEvent arg0) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
   */
  @Override
  public void windowDeiconified(WindowEvent arg0) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
   */
  @Override
  public void windowIconified(WindowEvent arg0) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
   */
  @Override
  public void windowOpened(WindowEvent arg0) {
  }
}
